/****************************************************************************************
 *
 * File Name: main 
 * Project Name: Stop0_Wakeup_Example
 * Version: v1.0
 * Date: 2022-03-20- 16:07:40
 * Author:
 * 
 ****************************************************************************************/
//#include<KF32A151MQV.h>
#include "system_init.h"
#include "usart.h"
#include "stop0.h"
#include "EXINT_user.h"
void delay_ms(volatile uint32_t nms)
{
    volatile uint32_t i, j;

    for (i = 0; i < nms; i++)
    {
        j = 7000;
        while (j--)
            ;
    }
}

//Main Function
int main()
{
    /* Init system clock, SCLK = 120M. */
    SystemInit();

    /* Init LED(PB13) GPIO. */
    GPIO_Write_Mode_Bits(GPIOB_SFR, GPIO_PIN_MASK_13, GPIO_MODE_OUT);
    GPIO_Set_Output_Data_Bits(GPIOB_SFR, GPIO_PIN_MASK_13, Bit_RESET); //LED OFF
    /*Init User Key.*/
    GPIO_Write_Mode_Bits(GPIOD_SFR, GPIO_PIN_MASK_7, GPIO_MODE_IN);
	/*Init Usart2.*/
	GPIO_USART();
	USART_Async_config(USART2_SFR);	// 19200
	/* Init WKUP interrupt */
	WKUP_Init();
	/* Init PMC interrupt. */
	PMC_Error_Init();
	/* Init stop0 mode */
	Stop0_Init();
	
    /* Enable global interrupt. */
    INT_All_Enable(TRUE);


	while(1)
	{
		delay_ms(1000);
		USART_Send(USART2_SFR , "MCU is running\r\n" ,sizeof("MCU is running\r\n"));
		GPIO_Toggle_Output_Data_Config(GPIOB_SFR, GPIO_PIN_MASK_13);
		if(GPIO_Read_Input_Data_Bit(GPIOD_SFR,GPIO_PIN_MASK_7) == SET)
		{
			delay_ms(5);
			if(GPIO_Read_Input_Data_Bit(GPIOD_SFR,GPIO_PIN_MASK_7) == SET)
			{
				USART_Send(USART2_SFR , "MCU is going into the mode of Stop0\r\n" ,sizeof("MCU is going into the mode of Stop0\r\n"));
				/* Goto stop0 mode. Must be running in RAM*/
				Goto_Stop0();
				/* Reinitialize system clock, SCLK = 120M. */
				SystemInit();
				USART_Send(USART2_SFR , "MCU has been awaked up\r\n" ,sizeof("MCU has been awaked up\r\n"));
			}

		}




	}		
}


